package com.atguigu.tingshu.user.strategy.impl;

import com.atguigu.tingshu.album.client.TrackInfoFeignClient;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.user.UserPaidTrack;
import com.atguigu.tingshu.user.mapper.UserPaidTrackMapper;
import com.atguigu.tingshu.user.strategy.ItemTypeStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author: hy
 * @date:2025/9/17
 * @description: {"1002":TrackStrategy}
 */
@Service("1002")
public class TrackStrategy implements ItemTypeStrategy {

    @Autowired
    private UserPaidTrackMapper userPaidTrackMapper;

    @Autowired
    private TrackInfoFeignClient trackInfoFeignClient;

    @Override
    public void savePaidRecord(UserPaidRecordVo userPaidRecordVo) {
        //购买声音 user_paid_track 判断当前购买的声音id在表中是否存在! 利用关键字continue
        //用户要购买的声音列表
        List<Long> itemIdList = userPaidRecordVo.getItemIdList();
        //用户已购买的声音列表
        LambdaQueryWrapper<UserPaidTrack> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserPaidTrack::getUserId, userPaidRecordVo.getUserId())
                .in(UserPaidTrack::getTrackId, itemIdList);
        //用户在已购买的列表中买了对应的声音
        List<UserPaidTrack> userPaidTrackList = userPaidTrackMapper.selectList(wrapper);
        //需要将这个集合变为set集合，通过trackId判断是否存在
        Set<Long> paidTrackIdSet = userPaidTrackList.stream().map(UserPaidTrack::getTrackId).collect(Collectors.toSet());

        //判空
        if (!CollectionUtils.isEmpty(userPaidTrackList)) {
            //有买过
            for (Long itemId : itemIdList) {
                if (paidTrackIdSet.contains(itemId)) {
                    //continue:结束本次循环，进入下次循环
                    //return:结束方法调用，返回方法调用处
                    //break:结束当前循环
                    //需要将包含的声音添加到集合中，用来记录哪些声音购买过
                    //使用mq进行增加余额
                    continue;
                }
            }

        }
        //没买过
        //根据声音id获取声音对象
        Result<TrackInfo> trackInfoResult = trackInfoFeignClient.getTrackInfo(userPaidRecordVo.getItemIdList().get(0));
        Assert.notNull(trackInfoResult, "查询声音信息失败");
        TrackInfo trackInfo = trackInfoResult.getData();
        Assert.notNull(trackInfo, "查询声音信息失败");
        for (Long trackId : itemIdList) {
            //创建对象保存数据
            UserPaidTrack userPaidTrack = new UserPaidTrack();
            userPaidTrack.setUserId(userPaidRecordVo.getUserId());
            userPaidTrack.setOrderNo(userPaidRecordVo.getOrderNo());
            userPaidTrack.setAlbumId(trackInfo.getAlbumId());
            userPaidTrack.setTrackId(trackId);
            //保存数据
            userPaidTrackMapper.insert(userPaidTrack);
        }
    }
}
